package com.eastfair.questionnaire.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.questionnaire.dto.RelationExternalAnswerDTO;
import com.eastfair.questionnaire.dto.RelationExternalQuestionDTO;
import com.eastfair.questionnaire.dto.RelationTagDTO;
import com.eastfair.questionnaire.service.RelationExternalAnswerService;
import com.eastfair.questionnaire.service.RelationTagService;
import com.eastfair.questionnaire.vo.AnswerVO;
import com.eastfair.questionnaire.vo.ExternalAnswerVO;
import com.eastfair.questionnaire.vo.InternalAnswerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 外部答案映射
 * </p>
 *
 * @author yuwx
 * @date 2021-08-18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/relationExternalAnswer")
@Api(value = "relationExternalAnswer", tags = "标签映射")
public class RelationExternalAnswerController {

    private RelationExternalAnswerService relationExternalAnswerService;
    private EchoService echoService;
    @Autowired
    public RelationExternalAnswerController(RelationExternalAnswerService relationExternalAnswerService, EchoService echoService) {
        this.relationExternalAnswerService = relationExternalAnswerService;
        this.echoService = echoService;
    }

    @ApiOperation(value = "保存答案、外部答案映射关系", notes = "保存答案、外部答案映射关系")
    @PostMapping("/saveAnswer")
    public R<Boolean> saveAnswer(
            @Validated({RelationExternalAnswerDTO.validateBase.class, RelationExternalAnswerDTO.validateSave.class})
            @RequestBody RelationExternalAnswerDTO relationExternalAnswer) {
        Long projectId = ContextUtil.getProjectId();
        relationExternalAnswerService.saveAnswer(projectId, relationExternalAnswer.getSystemSourceEnum()
                , new Pair<>(relationExternalAnswer.getInternalAnswerId(), relationExternalAnswer.getExternalAnswerId()));
        return R.successDef();
    }

    @ApiOperation(value = "保存问题、外部问题映射关系", notes = "保存问题、外部问题映射关系")
    @PostMapping("/saveQuestion")
    public R<Boolean> saveQuestion(
            @Validated({RelationExternalQuestionDTO.validateBase.class, RelationExternalQuestionDTO.validateSave.class})
            @RequestBody RelationExternalQuestionDTO relationExternalQuestion) {
        Long projectId = ContextUtil.getProjectId();
        relationExternalAnswerService.saveQuestion(projectId, relationExternalQuestion.getSystemSourceEnum()
                , new Pair<>(relationExternalQuestion.getInternalQuestionId(), relationExternalQuestion.getExternalQuestionId()));
        return R.successDef();
    }
    @ApiOperation(value = "内部答案转外部答案", notes = "内部答案转外部答案")
    @PostMapping("/convertInternalToExternal")
    public R<Map<String, List<ExternalAnswerVO>>> convertInternalToExternal(
            @Validated({RelationExternalAnswerDTO.validateBase.class, RelationExternalAnswerDTO.validateSave.class})
            @RequestBody RelationExternalAnswerDTO relationExternalAnswer) {
        Long projectId = ContextUtil.getProjectId();
        if (CollectionUtil.isEmpty(relationExternalAnswer.getInternalAnswerList())) {
            return R.success(Collections.emptyMap());
        }
        Map<String, List<ExternalAnswerVO>> convertResult = relationExternalAnswerService.convertInternalToExternal(projectId, relationExternalAnswer.getSystemSourceEnum()
                , relationExternalAnswer.getInternalAnswerList());
        return R.success(convertResult);
    }

    @ApiOperation(value = "外部答案转内部答案", notes = "外部答案转内部答案")
    @PostMapping("/convertExternalToInternal")
    public R<Map<Long, List<InternalAnswerVO>>> convertExternalToInternal(
            @Validated({RelationExternalAnswerDTO.validateBase.class, RelationExternalAnswerDTO.validateSave.class})
            @RequestBody RelationExternalAnswerDTO relationExternalAnswer) {
        Long projectId = ContextUtil.getProjectId();
        if (CollectionUtil.isEmpty(relationExternalAnswer.getExternalAnswerList())) {
            return R.success(Collections.emptyMap());
        }
        Map<Long, List<InternalAnswerVO>> convertResult = relationExternalAnswerService.convertExternalToInternal(projectId, relationExternalAnswer.getSystemSourceEnum()
                , relationExternalAnswer.getExternalAnswerList());
        return R.success(convertResult);
    }
}
